package com.refresh;

import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ScrollView;

/**
 * 
 * 判断是否可以下拉
 * 
 */
public abstract class PtrDefaultHandler implements PtrHandler {

    public static boolean canChildScrollUp(View view) {
        if (android.os.Build.VERSION.SDK_INT < 14) {

            if (view instanceof AbsListView) {
                final AbsListView absListView = (AbsListView) view;
                return absListView.getChildCount() > 0
                                && (absListView.getFirstVisiblePosition() > 0 || absListView
                                                .getChildAt(0).getTop() < absListView
                                                .getPaddingTop());
            } else {
                return view.getScrollY() > 0;
            }

        } else {
            ViewGroup viewGroup = (ViewGroup) view;
            if (viewGroup instanceof ScrollView) {

                if (viewGroup.getScrollY() == 0) {
                    return false;
                } else {
                    return true;
                }
            } else if (view instanceof AbsListView) {
                final AbsListView absListView = (AbsListView) view;
                return absListView.getChildCount() > 0
                                && (absListView.getFirstVisiblePosition() > 0 || absListView
                                                .getChildAt(0).getTop() < absListView
                                                .getPaddingTop());

            } else {
                return view.getScrollY() > 0;
            }

        }
    }

    public static boolean checkContentCanBePulledDown(PtrFrameLayout frame, View content,
                    View header) {
        return !canChildScrollUp(content);
    }

    @Override
    public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
        return checkContentCanBePulledDown(frame, content, header);
    }
}
